package com.jerry.android.VendorDB.Realm;

import com.jerry.android.Common.MyApplication;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by wangjun on 2018/1/2.
 */

public class RealmHelper {

    private String dbName = "my.realm";
    private int dbVersion = 1;

    private static RealmHelper instance;

    RealmHelper() {
        Realm.init(MyApplication.getContext());
    }

    public static RealmHelper getInstance() {
        if (instance == null){
            synchronized (RealmHelper.class){
                instance = new RealmHelper();
            }
        }
        return instance;
    }

    public Realm getRealm() {
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(dbName) //指定数据库的名称。如不指定默认名为default。
                .schemaVersion(dbVersion) //指定数据库的版本号。
                .deleteRealmIfMigrationNeeded()   //声明版本冲突时自动删除原数据库。
                .build();
        return Realm.getInstance(config);
    }
}
